library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 9.3
summary(factor(df$Q5B))
summary(factor(df$Q5C))
df2 <- df

# Important
df2$Q5B[df2$Q5B>3] <- NA
df2$Q5B[df2$Q5B==3] <- 0
df2$Q5B[df2$Q5B==2] <- 0.5
df2$Q5B[df2$Q5B==1] <- 1

df2$Q5C[df2$Q5C>3] <- NA
df2$Q5C[df2$Q5C==3] <- 0
df2$Q5C[df2$Q5C==2] <- 0.5
df2$Q5C[df2$Q5C==1] <- 1

colnames(df2[,c(26,27)]) # 26,27

df2 <- df2[df2$Q11 < 5 & df2$P_ASSIGN1==1,]

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

f.Q5B <- svyby(~ Q5B, ~Q11, w2_design, svymean, na.rm = TRUE)
f.Q5C <- svyby(~ Q5C, ~Q11, w2_design, svymean, na.rm = TRUE)

# Redressing Discrimination
g.5B <- ggplot(f.Q5B, aes(x=factor(Q11),y=Q5B,label=round(Q5B,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Military in Redressing Discrimination\n")

# Disaster Relief
g.5C <- ggplot(f.Q5C, aes(x=factor(Q11),y=Q5C,label=round(Q5C,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Disaster Relief Missions\n")

grid.arrange(g.5B,g.5C,ncol=2)
